package com.lzw.DailyAudit.service.dateAggregation_02;


import com.hooya.fa.eu.biz.DailyAudit.entity.DailyAuditStatus;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyAuditStatusMapper;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyMetaProductMapper;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyStkOrigDataCollectMapper;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyXsjl2303Mapper;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class Reset_Sku_Flag {

    @Autowired
    private DailyStkOrigDataCollectMapper stkOrigdataCollectMapper;

    @Autowired
    private DailyXsjl2303Mapper xsjl2303Mapper;

    @Autowired
    private DailyMetaProductMapper metaProductMapper;

	@Autowired
	private DailyAuditStatusMapper statusMapper;

	@Autowired
	private CommonService commonService;

	public void reset_sku_flag(String currentDate) {
		try {

			String name = "reset_sku_flag-02";
			DailyAuditStatus dailyAuditStatus = statusMapper.selectStatus(name, currentDate);
			if(dailyAuditStatus != null && dailyAuditStatus.getFlag() == 1){
				log.info("{} - 该方法已执行，进行跳过！",name);
				return;
			}

			log.info("开始执行重置SKU标记任务，日期为：{}", currentDate);

            // 获取符合条件的SKU列表
            List<String> stkSkuList = stkOrigdataCollectMapper.selectSkuList(currentDate);

            //这个是在rxh表中找指定时间的sku但是py流程中没有使用到它
//            List<String> rxhSkuList = xsjl2303Mapper.selectSkuList(startDate,endDate);

            // 重置所有needFlag为0
            int resetCount = metaProductMapper.resetNeedFlagToZero();
            log.info("重置了{}条记录的needFlag为0", resetCount);

            // 批量更新needFlag为1
            int count = metaProductMapper.batchUpdateNeedFlagToOne(stkSkuList);
            log.info("批量更新了{}条记录的needFlag为1", count);

			// 修改执行状态
			commonService.UpdateOrInsertStatus(dailyAuditStatus, name, currentDate);

			log.info("重置SKU标记任务完成");
			XxlJobHelper.log("重置SKU标记任务完成");
        } catch (Exception e) {
            log.error("重置SKU标记任务失败", e);
			XxlJobHelper.log("重置SKU标记任务失败");
            throw new RuntimeException("重置SKU标记任务失败", e);
        }
    }
}